<?php

	class source
	{

		var $fw		= null;
		var $sec	= null;

		function source(&$framework, &$security)
		{

			$this->fw	= &$framework;
			$this->sec	= &$security;

			$this->fw->pc_item('Wishlist', 'Movies you want to see or own', 'index.php?what=wishlist');

			switch($this->fw->do)
			{

				case 'remove':
					$this->remove_wishlist();
					break;

				case 'add':
					$this->add_wishlist();
					break;

				default:
					$this->list_wishlist();
					break;

			}

		}

		function add_wishlist()
		{

			$id	= isset($_SESSION['id']) ? $_SESSION['id'] : @$_REQUEST['id'];
			unset($_SESSION['id']);
			$id	= is_array($id) ? $id : array($id);

			$member_id	= $this->sec->dtls['id'];

			$i = array();

			foreach($id as $v)
			{

				// Make sure movie id doesnt already exist in the wishlist & current movie collection
				$sql		= $this->fw->_pq("SELECT COUNT(member_id) FROM #__own WHERE member_id = '%s' AND movie_id = '%s'", $member_id, $v);
				$result		= mysql_query($sql) or die(mysql_error());
				$own_num	= mysql_result($result, 0, 0);

				$sql		= $this->fw->_pq("SELECT COUNT(member_id) FROM #__wishlist WHERE member_id = '%s' AND movie_id = '%s'", $member_id, $v);
				$result		= mysql_query($sql) or die(mysql_error());
				$wish_num	= mysql_result($result, 0, 0);

				if($own_num == 0 && $wish_num == 0)
				{

					$sql	= $this->fw->_pq("INSERT INTO #__wishlist (member_id, movie_id, wishlist_created) VALUES ('%s', '%s', UNIX_TIMESTAMP())", $member_id, $v);
					$result	= mysql_query($sql) or die(mysql_error());

					$movie	= $this->fw->get_movie_details($v);

					if($result) $i[] = $movie['title'];

				}
				else
				{

					// This movie is already owned or on wishlist for this user

				}


			}

			if(count($i) > 0)
			{

				$_SESSION['msg']	= 'The following movie/s have been added to your wishlist.<br />';
				foreach($i as $v) $_SESSION['msg']	.= $v .'<br />';

			}
			else
			{

				$_SESSION['msg']	= 'No movies added to wishlist. They already exist, or are in your collection';

			}
			//header('location: index.php');
			header('location: '. $this->fw->referer);
			exit;

		}

		function list_wishlist()
		{

			$member_id	= $this->sec->dtls['id'];

			$this->fw->template	= 'wishlist.list.tpl';

			$filter	= trim(@$_REQUEST['filter']);
			$genre	= trim(@$_REQUEST['genre']);
			$search	= trim(@$_REQUEST['search']);

			// paginiation
			$num	= $this->fw->get_count_movies_in_wishlist($member_id, $filter, $search, $genre);
			$pagin	= $this->fw->pagination($num);

			$this->fw->content['movies']		= $this->fw->get_member_wishlist($member_id, $filter, $search, $genre, $pagin);
			$this->fw->content['pagination']	= $pagin;
			$this->fw->content['filter']		= $filter;
			$this->fw->content['genre']			= $genre;
			$this->fw->content['search']		= htmlentities($search);

		}

		function remove_wishlist()
		{

			// --= Remove selected MOVIE id's from the current members collection!

			$id			= @$_REQUEST['id'] or header('location: '. $this->fw->referer);
			$id			= is_array($id) ? $id : array($id);

			$member_id	= (int)$this->sec->dtls['id'];

			$i			= 0;

			foreach($id as $v)
			{

				$sql	= $this->fw->_pq("DELETE FROM #__wishlist WHERE member_id = %s AND movie_id = %s", $member_id, (int)$v);
				$result	= mysql_query($sql) or die(mysql_error());

				$i		+= mysql_affected_rows();

			}

			$_SESSION['msg']	= $i .' Movie(s) successfully removed from your wishlist';
			header('location: index.php?what=wishlist');
			exit;

		}

	}

?>